/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.actionControll {
	import com.googlecode.milib.base.MLActionClass;
	import com.googlecode.milib.service.singleton.EnterFrameService;
	import com.googlecode.milib.vo.MLEvent;

	/**
	 * @author Marek Brun @ MindChili
	 */
	public class ActionsMiscEnterFrameDetonator extends MLActionClass {

		private var actionsToStart:Array;
		private var efs:EnterFrameService;
		private var delayAndAction:Array;
		private var countdownDelayToStart:uint;
		private var actionToStart:MLActionClass;
		public var isStart:Boolean=true;
		public var isReverse:Boolean=false;
		public var isWaitToLastActionFinish:Boolean=false;		public var isDBG:Boolean=false;
		
		public function ActionsMiscEnterFrameDetonator() {
			delayAndAction=[];
			efs=EnterFrameService.getInstance();
			
		}
		
		override public function getPrivateVariables():Array {
			return [
				['delayAndAction', delayAndAction],
				['actionsToStart', actionsToStart]
			].concat(super.getPrivateVariables());
		}
		
		public function addAction(action:MLActionClass, delay:uint):void {
			delayAndAction.push([action, delay]);
		}
		
		override protected function doStart():Boolean {
			if(isDBG){
				
			}
			actionsToStart=delayAndAction.concat();
			if(isReverse){
				actionsToStart.reverse();
			}
			if(startNextAction()){
				efs.addEventListener(efs.event_EnterFrame, onEnterFrame);
				dbg.log('doStart true');
				return true;
			}
			return false;
		}
		
		public function cancel():void {
			actionsToStart=[];
			actionToStart=null;
			finish();
		}
		
		override protected function doFinish():Boolean {
			if(actionsToStart.length || actionToStart){ return false; }
			efs.removeEventListener(efs.event_EnterFrame, onEnterFrame);
			return true;
		}
		
		protected function startNextAction():Boolean {
			if(actionsToStart.length){
				var delayAndActionToStart:Array;
				while(true){
					delayAndActionToStart=actionsToStart.shift() as Array;
					countdownDelayToStart=uint(delayAndActionToStart[1]);
					actionToStart=MLActionClass(delayAndActionToStart[0]);
					if(countdownDelayToStart<=0){
						if(isStart){
							actionToStart.start();
						}else{
							actionToStart.finish();
						}
						if(actionsToStart.length==0){
							break;
						}
					}else{
						break;
					}
				};
				return true;
			}
			return false;
		}
		
//********************************************************************************************
//		events for ActionsEnterFrameDetonator
//********************************************************************************************
		protected function onEnterFrame(event:MLEvent):void {
			countdownDelayToStart--;
			if(countdownDelayToStart<=0) {
				if(isStart){
					actionToStart.start();
				}else{
					actionToStart.finish();
				}
				startNextAction();
				if(!actionsToStart.length){
					efs.removeEventListener(efs.event_EnterFrame, onEnterFrame);
					if(!isStart && isWaitToLastActionFinish){
						if(actionToStart.getIsRunning()) {
							actionToStart.addEventListener(actionToStart.event_ActionFinish, onLastActionToStartActionFinish);
							actionToStart.finish();
						}else{
							actionToStart=null;
							finish();
						}
					}else{
						if(isStart){
							actionToStart.start();
						}else{
							actionToStart.finish();
						}
						actionToStart=null;
						finish();
					}
				}
			}
		}
		
		protected function onLastActionToStartActionFinish(event:MLEvent):void {
			MLActionClass(event.info.hero).removeEventListener(MLActionClass(event.info.hero).event_ActionFinish, onLastActionToStartActionFinish);
			actionToStart.removeEventListener(actionToStart.event_ActionFinish, onLastActionToStartActionFinish);
			actionToStart=null;
			finish();
		}
		
	}
}
